package com.example.mykotilin.room.views


import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.example.mykotilin.R
import com.example.mykotilin.room.ViewModelFactory
import com.example.mykotilin.room.viewmodel.UserViewModel
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.example.mykotilin.room.Injection
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers


/**
 * 需要注意的坑
 * 1. private val viewModel: UserViewModel by viewModels {
 * viewModels无法解析  必须都要添加
 *    implementation "androidx.activity:activity:1.4.0"
*     implementation "androidx.fragment:fragment-ktx:1.4.0"
 * 2. 出现guava 双份依赖时候
 *    implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
 * 3. cannot find implementation for XXX. XXX_Impl does not exist
 *    将 annotationProcessor "androidx.room:room-compiler:$room_version"修改为
 *     kapt "androidx.room:room-compiler:$room_version"
 *     同时为为了可以应用kapt应该添加 apply plugin:'kotlin-kapt'
 */
class UserActivity : AppCompatActivity() {
    private lateinit var viewModelFactory: ViewModelFactory;
    private val viewModel: UserViewModel by viewModels {
        viewModelFactory
    }
    private val disposable = CompositeDisposable()

    private lateinit var update_user_button:Button
    private lateinit var user_name:TextView
    private lateinit var user_name_input:EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)
        //创建数据库
        viewModelFactory = Injection.provideViewModelFactory(context = this)

        update_user_button = findViewById(R.id.update_user_button)
        user_name = findViewById(R.id.user_name)
        user_name_input = findViewById(R.id.user_name_input)

        update_user_button.setOnClickListener{
            updateUserName()
        }
    }

    override fun onStart() {
        super.onStart()
        disposable.add(viewModel.userName().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()).subscribe(
            {
                this.user_name.text=it
            },{
                error->Log.e(TAG,"Unable to get Username",error)
            }
        ))
    }

    override fun onStop() {
        super.onStop()
        disposable.clear()
    }

    private fun updateUserName(){
        val userName = user_name_input.text.toString()
        update_user_button.isEnabled = false
        disposable.add(viewModel.updateUserName(userName)
            .subscribeOn(Schedulers.io()).
            observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                update_user_button.isEnabled = true
            },{
                error->
                Log.e(TAG,"unable to upadate username",error)
            })
        )
    }

    companion object{
        private val TAG = UserActivity::class.java.simpleName
    }
}